<template>
 <div :style="styleObj">
    <v-chart  :options="options" autoresize/>
  </div>
</template>

<script>
export default {
  name: "widgetBarStacked",
   props: {
    value: Object,
    ispreview: Boolean
  },
  data() {
    return {
      optionsStyle: {}, // 样式
      optionsData: {}, // 数据
      optionsCollapse: {}, // 图标属性
      optionsSetup: {},
      options:
{
        legend: {
          orient:'vertical',
          show: true,
          left:'0',
          bottom:'50%',
        },
        angleAxis: {
          max: 100,
          axisLine:{
            show:false
          },
          axisTick: {
            show: false
          },
          minorTick:{
            show:false
          }, 
          axisLabel:{
            show:false
          },
          splitLine:{
            show:false
          },
        },
        radiusAxis: {
          type: 'category',
          z: 10,
          axisLine:{
          show:false
          } ,
          splitLine:{
            show:false
          },
          axisTick:{
            show:false
          },
          minorTick:{
            show:false
          }, 
          axisLabel:{
            show:false
          },
        },
        polar: {},
        series: []
      }
    };
  },
  computed: {
    styleObj() {
      return {
        position: this.ispreview ? "absolute" : "static",
        width: this.optionsStyle.width + "px",
        height: this.optionsStyle.height + "px",
        left: this.optionsStyle.left + "px",
        top: this.optionsStyle.top + "px",
        background: this.optionsSetup.background
      };
    }
  },
  watch: {
    value: {
      handler(val) {
        this.optionsStyle = val.position;
        this.optionsData = val.data;
        this.optionsCollapse = val.collapse;
        this.optionsSetup = val.setup;
        this.editorOptions();
      },
      deep: true
    }
  },
  created() {
    this.optionsStyle = this.value.position;
    this.optionsData = this.value.data;
    this.optionsCollapse = this.value.collapse;
    this.optionsSetup = this.value.setup;
    this.editorOptions();
  },
  methods: {
  // 修改图标options属性
    editorOptions() {
      this.setOptionsColor();
      this.setOptionsData();
    },
    // 图例颜色修改
    setOptionsColor() {
      const optionsSetup = this.optionsSetup;
      const customColor = optionsSetup.customColor;
      if (!customColor) return;
      const arrColor = [];
      for (let i = 0; i < customColor.length; i++) {
        arrColor.push(customColor[i].color);
      }
      this.options.color = arrColor;
      this.options = Object.assign({}, this.options);
    },
     // 处理数据
     setOptionsData() {
      const optionsData = this.optionsData; // 数据类型 静态 or 动态
      optionsData.dataType == "staticData"
        ? this.staticDataFn(optionsData.staticData)
        : this.dynamicDataFn(optionsData.dynamicData, optionsData.refreshTime);
    },
    staticDataFn(val) {
     this.setSeriesData(val);
    },
    dynamicDataFn(val, refreshTime) {
      if (!val) return;
      if (this.ispreview) {
        this.getEchartData(val);
        this.flagInter = setInterval(() => {
          this.getEchartData(val);
        }, refreshTime);
      } else {
        this.getEchartData(val);
      }
    },
    getEchartData(val) {
      const data = this.queryEchartsData(val);
      data.then(res => {
        this.renderingFn(res);
      });
    },
    renderingFn(val) {
        this.setSeriesData(val);
    },
    setSeriesData(val){
     
      const sortData = [...val];
      sortData.sort((item1, item2) => item1.value - item2.value)

      let colorList = this.options.color;

      let max = 0; //最大值
      let legendData = []; // 项
      let radiusAxisData = []
      const newData = sortData.map((item,index) =>{
        let name = item.name;
        let value = item.value;
        //获取最大值
        max = value < max?max:value;
        //创建数组并附值
        let itemData = new Array(sortData.length);
        itemData[index] = item.value
        
        let textStyle = {
            color:colorList[index%colorList.length]
          }
        radiusAxisData.push({
          value: name,
          textStyle:{
            ...textStyle,
          }
        })
        legendData.push({
          name: name,
          textStyle:textStyle
        });
        return {
              type: 'bar',
              data: itemData,
              coordinateSystem: 'polar',
              barGap: "45%",
              stack: 'a',
              name: item.name,
              emphasis: {
                focus: 'series'
              },
               label: {
                show: true,
                position: 'middle',
                color:'#FFFFFF'
              }
            }
      })
      this.options.series = newData
      this.options.radiusAxis.data = radiusAxisData;
      this.options.legend.data = legendData;
      this.options.angleAxis.max = max * 4/3;
      console.log('newData',newData)
    }
  }
};
</script>

<style scoped lang="scss">
.echarts {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
</style>
